package com.andy.service;

import java.io.InputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.andy.dao.IExamRoleDAO;
import com.andy.dao.IExamUserDAO;
import com.andy.entity.ExamRole;
import com.andy.entity.ExamUser;
import com.andy.utils.ExcelUtils;
import com.andy.utils.MD5Utils;
import com.andy.vo.ExamUserVO;

@Service
@Transactional
public class ManageStudentService implements IManageStudentService {
	
	@Autowired
	private IExamUserDAO examUserDAO;
	
	@Autowired
	private IExamRoleDAO examRoleDAO;

	/**
	 * 获取所有学生信息
	 * @return
	 */
	@Override
	public List<ExamUserVO> getAllStudents() {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		List<ExamUserVO> resultStudentList = new ArrayList<ExamUserVO>();
		List<ExamUser> studentList = examUserDAO.getAllStudent();
		ExamUserVO vo = null;
		if (studentList != null) {
			for (ExamUser user : studentList) {
				vo = new ExamUserVO();
				BeanUtils.copyProperties(user, vo);
				vo.setBirthdayString(df.format(user.getBirthday()));
				resultStudentList.add(vo);
			}
		}
		return resultStudentList;
	}

	/**
	 * 根据Excel内容，批量添加学生
	 * @param inputStream
	 * @param postfix
	 */
	@Override
	public void saveStudentByExcel(InputStream inputStream, String postfix) {
		Workbook wk = ExcelUtils.openWorkbook(inputStream, postfix);
		List<ExamUserVO> allExcelUser = ExcelUtils.getStudentListByExcel(wk);
		List<ExamUser> entities = new ArrayList<ExamUser>();
		if (allExcelUser != null) {
			ExamRole role = examRoleDAO.get(25);
			ExamUser entity = null;
			for (int i = 0; i < allExcelUser.size(); i ++) {
				entity = new ExamUser();
				BeanUtils.copyProperties(allExcelUser.get(i), entity);
				Set<ExamRole> examRoles = new HashSet<ExamRole>();
				examRoles.add(role);
				entity.setExamRoles(examRoles );
				entities.add(entity);
			}
		}
		examUserDAO.save(entities);
	}

	/**
	 * 添加学生
	 * @param examUserVO
	 * @throws ParseException
	 */
	@Override
	public void addStudent(ExamUserVO examUserVO) throws ParseException {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		if (examUserVO != null) {
			ExamRole role = examRoleDAO.get(25);
			ExamUser entity = new ExamUser();
			BeanUtils.copyProperties(examUserVO, entity);
			Set<ExamRole> examRoles = new HashSet<ExamRole>();
			examRoles.add(role);
			entity.setBirthday(df.parse(examUserVO.getBirthdayString()));	// 转换String类开的日期为Date类型
			entity.setExamRoles(examRoles );	// 设置关联学生角色
			entity.setPassword(MD5Utils.GetMD5Code(examUserVO.getPassword()));
			examUserDAO.save(entity);
		}
		
	}

	/**
	 * 修改学生信息
	 * @param examUserVO
	 * @throws ParseException 
	 */
	@Override
	public void updateStudent(ExamUserVO examUserVO) throws ParseException {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		if (examUserVO != null) {
			ExamUser existsUser = examUserDAO.get(examUserVO.getUserId());
			if (existsUser != null) {
				existsUser.setUserName(examUserVO.getUserName());
				existsUser.setBirthday(df.parse(examUserVO.getBirthdayString()));	// 转换String类开的日期为Date类型);
				existsUser.setAddress(examUserVO.getAddress());
				existsUser.setEmail(examUserVO.getEmail());
				existsUser.setGender(examUserVO.getGender());
				existsUser.setTel(examUserVO.getTel());
				ExamRole role = examRoleDAO.get(25);
				Set<ExamRole> examRoles = new HashSet<ExamRole>();
				examRoles.add(role);
				existsUser.setExamRoles(examRoles );
				examUserDAO.update(existsUser);
			} else {
				throw new RuntimeException("不存在数据。");
			}
		}
	}

	/**
	 * 根据学号删除学生
	 * @param userId
	 */
	@Override
	public void deleteStudent(String userId) {
		if (userId != null) {
			ExamUser existsUser = examUserDAO.get(userId);
			if (existsUser != null) {
				examUserDAO.delete(existsUser);
			} else {
				throw new RuntimeException("不存在数据。");
			}
		}
	}

	/**
	 * 根据学号获取学生信息
	 * @param userId
	 * @return
	 */
	@Override
	public ExamUserVO getStudentById(String userId) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		if (userId != null) {
			ExamUser entity = examUserDAO.get(userId);
			if (entity != null) {
				ExamUserVO resultVO = new ExamUserVO();
				BeanUtils.copyProperties(entity, resultVO);
				resultVO.setBirthdayString(df.format(entity.getBirthday()));
				return resultVO;
			}
		}
		return null;
	}

}
